﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Slot : MonoBehaviour, IDropHandler {
	public GameObject inventory;
	public GameObject item {
		get {
			if(transform.childCount > 0) 
				return transform.GetChild(0).gameObject;
			else
				return null;
		}
	}
	void Start() {
		inventory = GameObject.Find("GameStateManager");
	}
	#region IDropHandler implementation

	public void OnDrop (PointerEventData eventData)
	{
		if(inventory.GetComponent<Inventory>().canSwap(DragHandler.initialParent.gameObject, gameObject)) {
			inventory.GetComponent<Inventory>().SwapSlots(DragHandler.initialParent.gameObject, gameObject);
			if(!item) {
				DragHandler.objectBeingDragged.transform.SetParent(transform);
			}
			else {
				item.transform.SetParent(DragHandler.initialParent);
				DragHandler.objectBeingDragged.transform.SetParent(transform);
			}
			DragHandler.initialParent.GetComponent<GridLayoutGroup>().enabled = true;
			DragHandler.initialParent = null;
		}
	}

	#endregion
}
